<template>
  <div class="apply-dialog">
    <div v-if="thisDiv === 1 || thisDiv === 2">
      <div class="title" style="display: flex;">
        <div style="flex: 1; text-align: left;padding-left: 30px;font-size: 25px;">
          {{ models.eventName }}
        </div>
        <div class="btn-list flex flex-row align-center">
          <el-button type="warning" size="mini" :loading="saveLoading" @click="saveAction">暂存</el-button>
          <el-button type="primary" size="mini" :loading="submitLoading" @click="submitAction">上报</el-button>
          <el-button type="info" size="mini" @click="handleClose">返回</el-button>
        </div>
      </div>
      <el-form
        ref="generateForm"
        label-suffix=":"
        size="small"
        :model="models"
        :rules="rules"
        label-position="right"
        label-width="130px"
      >
        <div >
          <title-info title="上报⼈信息" >
            <reporter-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="当事人信息" >
            <party-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="当事患者信息" >
            <patient-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="事件信息" >
            <event-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="事件差错详情" >
            <div slot="left" style="display: flex; width: 100%; flex-wrap: wrap; ">
              <el-form-item label="操作类型" prop="faultOperationTypeArr" label-width="150px">
                <div style="display: flex; width: 100%">
                  <el-checkbox-group v-model="models.faultOperationTypeArr">
                    <el-checkbox v-for="(item, index) in operationTypeList" :key="index" :label="item.value">
                      <div style="display: flex;align-items: center;">
                        <div style="display: flex;align-items: center;">
                          {{ item.label }}
                        </div>
                        <!--        二维数据              -->
                        <div v-if="models.faultOperationTypeArr && models.faultOperationTypeArr.indexOf('582') > -1 && item.value === '582'" style="margin-left: 5px;display: flex;align-items: center;">
                          (
                          <el-checkbox-group v-model="models.faultOperationTypeSampleArr">
                            <el-checkbox v-for="(item, index) in faultOperationTypeSampleList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                          </el-checkbox-group>
                          <el-input
                            v-if="models.faultOperationTypeSampleArr && models.faultOperationTypeSampleArr.indexOf('12')  > -1 && item.value === '582'"
                            :style="{ width: '200px', 'margin-left': '5px', 'align-items': 'center' }"
                            placeholder="请输入"
                            v-model="models.baqsjRecordDiagnosisSampleWrong.faultOperationTypeSampleOther"
                          />
                          )
                        </div>
                      </div>
                    </el-checkbox>
                    <el-input
                      v-if="models.faultOperationTypeArr && models.faultOperationTypeArr.indexOf('586')  > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordDiagnosisSampleWrong.faultOperationTypeOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
              <div style="width: 100%" />
              <el-form-item label="操作是否为侵入性" prop="invasionTypeArr" label-width="150px">
                <el-checkbox-group v-model="models.invasionTypeArr">
                  <el-checkbox v-for="(item, index) in isInvasiveList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                </el-checkbox-group>
              </el-form-item>
              <div style="width: 100%" />
              <el-form-item label="错误类型" prop="sampleWrongTypeArr" label-width="150px">
                <div style="display: inline-block; width: 100%">
                  <el-checkbox-group v-model="models.sampleWrongTypeArr" style="display: inline-block">
                    <el-checkbox
                      v-for="(item, index) in errorTypeList"
                      :key="index"
                      :label="item.value"
                    >
                      {{ item.label }}
                    </el-checkbox>
                    <el-input
                      v-if="models.sampleWrongTypeArr && models.sampleWrongTypeArr.indexOf('607') > -1"
                      :style="{ width: '200px', 'margin-left': '5px', display: 'inline-block' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordDiagnosisSampleWrong.sampleTypeOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
            </div>
          </title-info>
        </div>
        <div >
          <title-info title="处理措施" >
            <measure-one-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="其他信息说明" >
            <other-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
      </el-form>
    </div>
    <div v-if="thisDiv === 3">>
      <div>
        <title-info title="上报⼈信息" >
          <reporter-info slot="left" :form-data="models" :this-div="thisDiv" />
        </title-info>
        <div >
          <title-info title="当事人信息" >
            <party-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="当事患者信息" >
            <patient-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="事件信息" >
            <event-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="" >
            <div slot="left">
              <table>
                <tr>
                  <td class="td-title">操作类型</td>
                  <td>{{ getOperationTypes(models.faultOperationTypeArr) || '' }}</td>
                  <td class="td-title">操作是否为侵入性</td>
                  <td>{{ getInvasionTypes(models.invasionTypeArr) || '' }}</td>
                  <td class="td-title">错误类型</td>
                  <td>{{ getErrorTypes(models.sampleWrongTypeArr) || '' }}</td>
                </tr>
              </table>
            </div>
          </title-info>
        </div>
        <div >
          <title-info title="处理措施" >
            <measure-one-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="其他信息说明" >
            <other-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { doSaveDiagnosisSampleWrong } from '@/api/report/reportForm'

import titleInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/titleInfo.vue'
import reporterInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/reporterInfo.vue'
import partyInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/partyInfo.vue'
import patientInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/patientInfo.vue'
import eventInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/eventInfo.vue'
import measureOneInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/measureOneInfo.vue'
import otherInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/otherInfo.vue'

export default {
  name: 'DiagnosisSampleWrong',
  components: { titleInfo, reporterInfo, partyInfo, patientInfo, eventInfo, measureOneInfo, otherInfo },
  data() {
    return {
      // 当前显示的div：1 dialog 2 详情 3 处置
      thisDiv: 0,
      models: {},
      loading: false,
      dialogVisible: false,
      dialogDetailVisible: false,
      submitLoading: false,
      saveLoading: false,
      rules: {},
      operationTypeList: [
        { value: '580', label: '检查类' },
        { value: '581', label: '标本类（病理标本）' },
        { value: '582', label: '标本类（检验标本）' },
        { value: '583', label: '处置（如包扎、⽌⾎）、治疗类' },
        { value: '584', label: '检验类' },
        { value: '585', label: '不明' },
        { value: '586', label: '其他' }
      ],
      // 10-血，11-尿，12-其他
      faultOperationTypeSampleList: [
        { value: '10', label: '血' },
        { value: '11', label: '尿' },
        { value: '12', label: '其他' }
      ],
      isInvasiveList: [
        { value: '3', label: '是' },
        { value: '2', label: '否' },
        { value: '1', label: '不明' }
      ],
      errorTypeList: [
        { value: '600', label: '标本未贴标签' },
        { value: '601', label: '标本标签错误' },
        { value: '602', label: '标本遗失' },
        { value: '603', label: '收集标本的⽅法不正确' },
        { value: '604', label: '未做检查/检验' },
        { value: '605', label: '处置问题' },
        { value: '606', label: '发错检查/检验报告（包括错发、延迟、誊写等）' },
        { value: '607', label: '其他' }
      ]
    }
  },
  methods: {
    handleClose(val) {
      this.dialogVisible = false
      // 传到父组件
      this.$emit('close', val)
    },
    handleDetailClose() {
      this.dialogDetailVisible = false
    },
    getDetail(info) {
      this.models = info
      this.models = {
        ...info,
        medicalInvolvedArr: info.baqsjRecordHandle && info.baqsjRecordHandle.medicalInvolved ? info.baqsjRecordHandle.medicalInvolved.split(',') : [],
        otherInvolvedArr: info.baqsjRecordHandle && info.baqsjRecordHandle.otherInvolved ? info.baqsjRecordHandle.otherInvolved.split(',') : [],
        faultOperationTypeArr: info.baqsjRecordDiagnosisSampleWrong && info.baqsjRecordDiagnosisSampleWrong.faultOperationType ? info.baqsjRecordDiagnosisSampleWrong.faultOperationType.split(',') : [],
        faultOperationTypeSampleArr: info.baqsjRecordDiagnosisSampleWrong && info.baqsjRecordDiagnosisSampleWrong.faultOperationTypeSample ? info.baqsjRecordDiagnosisSampleWrong.faultOperationTypeSample.split(',') : [],
        sampleWrongTypeArr: info.baqsjRecordDiagnosisSampleWrong && info.baqsjRecordDiagnosisSampleWrong.sampleWrongType ? info.baqsjRecordDiagnosisSampleWrong.sampleWrongType.split(',') : [],
        invasionTypeArr: info.baqsjRecordDiagnosisSampleWrong && info.baqsjRecordDiagnosisSampleWrong.invasionType ? info.baqsjRecordDiagnosisSampleWrong.invasionType.split(',') : []
      }
      console.log(this.models)
    },
    editInfo(ruleInfo, info) {
      this.thisDiv = 2
      this.dialogVisible = true
      this.rules = ruleInfo
      this.getDetail(info)
    },
    detail(info) {
      this.thisDiv = 3
      this.dialogDetailVisible = true
      this.getDetail(info)
    },
    open(ruleInfo, modelInfo) {
      // 弹出框
      this.thisDiv = 1
      this.rules = ruleInfo
      this.models = {
        ...modelInfo,
        eventNo: 'DiagnosisSampleWrong',
        eventName: '诊疗相关-报告异常，标本差错事件',
        // 其他复选框
        // 处理措施=>医疗介入：120-持续观察，121-加强护理防范，122-监测生命体征，123-伤口护理，124-给予额外关注(如：约束、注射治疗、药物治疗)，125-其他
        medicalInvolvedArr: [],
        //  处理措施=>其他介入：130-予以病人家属慰问及支持,131-移除与危害相关的设备或环境运作,32-上报警察机关,133-通报卫生主管机构,134-其他
        otherInvolvedArr: [],
        // 事件差错详情 =>操作类型 (不同的表单，定制不同)
        faultOperationTypeArr: [],
        // 事件差错详情 => 检验标本（需设置⼦选项：⾎、尿、其他）(不同的表单，定制不同)
        faultOperationTypeSampleArr: [],
        // 事件差错详情 => 错误类型(不同的表单，定制不同)
        sampleWrongTypeArr: [],
        // 事件差错详情 =>  操作是否为侵入性：1-是，2否，3-不明
        invasionTypeArr: []
      }
      this.dialogVisible = true
    },
    submitAction() {
      // 上报
      this.models.auditStatus = 2
      this.actionRequest()
    },
    saveAction(item) {
      // 草稿
      this.models.auditStatus = 1
      this.actionRequest()
    },
    actionRequest() {
      // 新增、编辑、暂存
      this.$refs['generateForm'].validate(valid => {
        if (valid) {
          this.$message({
            message: '正在操作请稍等...',
            type: 'warning'
          })
          this.saveLoading = true
          this.submitLoading = true
          this.models.baqsjRecordHandle.medicalInvolved = this.models.medicalInvolvedArr.join(',')
          this.models.baqsjRecordHandle.otherInvolved = this.models.otherInvolvedArr.join(',')
          this.models.baqsjRecordDiagnosisSampleWrong.faultOperationType = this.models.faultOperationTypeArr.join(',')
          this.models.baqsjRecordDiagnosisSampleWrong.faultOperationTypeSample = this.models.faultOperationTypeSampleArr.join(',')
          this.models.baqsjRecordDiagnosisSampleWrong.sampleWrongType = this.models.sampleWrongTypeArr.join(',')
          this.models.baqsjRecordDiagnosisSampleWrong.invasionType = this.models.invasionTypeArr.join(',')
          doSaveDiagnosisSampleWrong(this.models).then(res => {
            if (res.resStatus !== 500) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.saveLoading = false
              this.submitLoading = false
              // 关闭弹出框
              this.dialogVisible = false
              this.handleClose(1)
            } else {
              this.$message({
                type: 'error',
                message: '操作失败!'
              })
              this.saveLoading = false
              this.submitLoading = false
            }
          })
        } else {
          this.$message({
            message: '请填写必填项',
            type: 'warning'
          })
        }
      })
    },
    getOperationTypes(values) {
      if (!values || values.length === 0) return ''
      const labels = []
      this.operationTypeList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '582') {
            const sampleLabels = []
            this.faultOperationTypeSampleList.forEach(sampleItem => {
              if (this.models.faultOperationTypeSampleArr && this.models.faultOperationTypeSampleArr.includes(sampleItem.value)) {
                if (sampleItem.value === '12') {
                  sampleLabels.push(`其他（${this.models.baqsjRecordDiagnosisSampleWrong.faultOperationTypeSampleOther || ''}）`)
                } else {
                  sampleLabels.push(sampleItem.label)
                }
              }
            })
            if (sampleLabels.length > 0) {
              labels.push(`${item.label}(${sampleLabels.join(', ')})`)
            } else {
              labels.push(item.label)
            }
          } else if (item.value === '586') {
            labels.push(`其他（${this.models.baqsjRecordDiagnosisSampleWrong.faultOperationTypeOther || ''}）`)
          } else {
            labels.push(item.label)
          }
        }
      })
      return labels.join(', ')
    },
    getInvasionTypes(values) {
      if (!values || values.length === 0) return ''
      const labels = []
      this.isInvasiveList.forEach(item => {
        if (values.includes(item.value)) {
          labels.push(item.label)
        }
      })
      return labels.join(', ')
    },
    getErrorTypes(values) {
      if (!values || values.length === 0) return ''
      const labels = []
      this.errorTypeList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '607') {
            labels.push(`其他（${this.models.baqsjRecordDiagnosisSampleWrong.sampleTypeOther || ''}）`)
          } else {
            labels.push(item.label)
          }
        }
      })
      return labels.join(', ')
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  padding: 12px;
  border-bottom: 1px solid rgba(0, 0, 0, .1);
  position: sticky;
  top: 0;
  z-index: 2000;
  background-color: #fff;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
  table-layout: fixed;
}
td {
  border: 1px solid #ccc;
  padding: 10px;
  text-align: left;
  /* 调整行间距 */
  line-height: 1.6;
  /* 调整字体大小 */
  font-size: 18px;
  /* 调整字间距 */
  letter-spacing: 0.5px;
}

.td-title {
  width: 12%;
  background-color: #fff;
}
</style>
